package com.example.fengyungo.dao;

import com.example.fengyungo.domain.entity.Order;
import com.example.fengyungo.domain.vo.OrderDetailsVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface OrderDao {

    /**
     * 获取可接订单
     *
     * @return List<Order>
     */
    @Select("select * from tb_order where orderStatus = 0")
    List<Order> getAccessOrder();

    /**
     * 查询某位司机所接的订单
     *
     * @param receiverId Integer
     * @return List<Order>
     */
    @Select("select * from tb_order where receiverId = #{receiverId} order by orderStatus")
    List<Order> getOrdersByDriver(String receiverId);

    /**
     * 根据位置查询司机可接订单
     *
     * @param place String
     * @return List<Order>
     */
    @Select("select * from tb_order where begin like CONCAT('%', #{place}, '%') || end like CONCAT('%', #{place}, '%');")
    List<Order> getOrdersByPlace(String place);

    /**
     * 根据订单编号更新接单者id与订单状态
     *
     * @param receiverId 接单者id
     * @param number     订单编号
     * @return Boolean
     */
    @Update("UPDATE tb_order SET receiverId = #{receiverId} , orderStatus = 1 WHERE number = #{number}")
    Integer updateReceiverIdAndOrderStatusByNumber(@Param("receiverId") String receiverId, @Param("number") String number);

    /**
     * 用户发布订单（新增订单）
     *
     * @param order Order
     * @return Boolean结果
     */
    @Insert("insert into tb_order " +
            "(userId, receiverId, number, begin, end, earliestTime, latestTime, note, price, payStatus, orderStatus, credit) " +
            "VALUES " +
            "(#{userId},#{receiverId},#{number},#{begin},#{end},#{earliestTime},#{latestTime},#{note},#{price},#{payStatus},#{orderStatus},#{credit})")
    Boolean addOrder(Order order);

    /**
     * 根据用户id查询用户发布的订单
     *
     * @param userId 用户id
     * @return List<Order>
     */
    @Select("select * from tb_order where userId = #{userId} and orderStatus != 4 order by orderStatus")
    List<Order> getOrdersByUserId(String userId);

    /**
     * 根据订单编号修改订单
     *
     * @param order 订单详情
     * @return Integer
     */
    Integer updateByNumber(Order order);

    /**
     * 根据起点或终点查询司机可接的订单
     *
     * @param begin 起点
     * @param end   终点
     * @return List<Order>
     */
    List<OrderDetailsVO> getOrdersByPosition(@Param("begin") String begin, @Param("end") String end);

    /**
     * 根据订单编号更新订单的评分
     *
     * @param number 订单编号
     * @param credit 评分
     * @return Integer
     */
    @Update("UPDATE tb_order SET credit = #{credit} WHERE number = #{number}")
    Integer updateCredit(@Param("number") String number, @Param("credit") float credit);

    /**
     * 根据条件查询订单
     *
     * @param order 订单详情
     * @return List<OrderDetailsVO>
     */
    List<OrderDetailsVO> getOrdersByCondition(Order order);
}
